GBML manual / Lists

list_sort

Puts the items of target in order. Numbers go smallest first when ascending is true and biggest first when it is false. Text goes in alphabetical order when ascending is true (Z to A when it is false), with one catch: every capital letter comes before every small letter. It changes target itself and makes no copy. A list of yes-or-no values, structs or references is refused when the project is checked.

Syntax

list_sort(target, ascending)

Arguments

NameType
targetlist
ascendingbool

Returns

Nothing.

Description

Puts the items of target, a list, in order. For numbers, when ascending is true the smallest comes first, and when it is false the biggest comes first, which is the way a high-score table is usually shown. For text, true puts the A's first and false puts the Z's first (there is a catch about capital letters, below).

list_sort(scores, true);    // 10, 20, 30
list_sort(scores, false);   // 30, 20, 10
list_sort(names, true);     // "Ada", "Bo", "Cy"

It changes the list itself

list_sort rearranges the items that are already in target. It does not make a new list, and it needs no extra memory. The old order is gone afterwards. If you want to keep it, make a copy with list_copy first and sort the one you want sorted.

It sorts the items the list holds right now, the number list_length gives you. The empty places up to the capacity are not touched, and a list with no items or one item is left as it is.

Numbers and text

A list of whole numbers or decimals is put in number order, and a list of text is put in alphabetical order. A list of yes-or-no values, of structs or of references is refused when the project is checked, before the game runs: GBML does not know what order those should go in, so it will not guess.

The list holdsWhat happens
whole numbers (list<int>)sorted
decimals (list<real>)sorted
text (list<string>)sorted, in the order below
yes-or-no values, structs or referencesrefused when checked

If you only want the order mixed up, and not put in any order, list_shuffle works on a list of anything.

Text goes in alphabetical order, with a catch

Text is put in order one character at a time, by each character's code. The code is the number a character has in the table of letters ("A" is 65, "a" is 97, "0" is 48, a space is 32: see string_code_at). That gives these rules:

RuleExample
a space comes before a digit, a digit before a capital letter, a capital letter before a small letter" x", "10", "Zed", "ada" come in that order
every capital letter comes before every small letter. A capital and a small letter are not treated as the same letter"Zed" comes before "ada", and "Apple" before "apple"
digits are compared one at a time"10" comes before "9", because 1 comes before 9
a text that is the start of a longer one comes first"ab" comes before "abc"
a character past the plain keyboard ones (a code above 127) comes after all of themit comes last

If you want names to come out the way a dictionary would put them, write them all with a capital first letter, or all small. With ascending set to false the whole order is turned around.

What happens in the awkward cases

You do thisWhat happens
sort a list that has equal numbers, or equal pieces of text, in itthey end up next to each other. Two equal values are the same value, so it does not matter which is which
leave ascending out, or give a number instead of true or falserefused when checked
give it an array or a gridrefused when checked, and the message names the function that sorts that kind, like array_sort
give it a stack or a queuerefused when checked. A stack and a queue give things back in the order they went in, so sorting one would undo what it is for
use it where a value is neededrefused when checked. It gives nothing back

In a script

A script that takes a list as a parameter works on the caller's own list (see Lists). So list_sort inside a script sorts the caller's list.

What it costs

Nothing is set aside: the sort is done in place, one item at a time. The time grows a little faster than the number of items, and comparing two pieces of text takes longer than comparing two numbers. A list of a few dozen items is nothing to worry about, but sorting a very long list every frame is something to avoid. No timing was measured.

Example

/// @type {list<int>}
var scores = [30, 10, 20];
list_sort(scores, true);     // 10, 20, 30
var lowest = scores[0];      // 10
list_sort(scores, false);    // 30, 20, 10
var highest = scores[0];     // 30
/// @type {list<string>}
var names = ["pear", "Zed", "ada"];
list_sort(names, true);      // "Zed", "ada", "pear": a capital letter comes before a small one

On the GBA

Sorts the list_length(target) items the list holds - never its capacity - with a shell sort over the vector, in place. A shell sort done in place: it uses no extra memory, makes no copy, and never leaves the places that hold items. It is not stable, which cannot show for numbers or text (two equal values are the same value). The time grows a little faster than the number of items - about n times the square root of n in the worst case, usually much less - and comparing two texts takes longer than comparing two numbers (as many steps as the two texts have in common at the start). No timing was measured. Whole numbers, decimals and text build. Text is put in order one character at a time, by each character's code. A space comes before a digit, a digit before a capital letter and a capital letter before a small one, so "Zed" comes before "ada" (a capital and a small letter are NOT treated as the same letter), and a text that is the start of a longer one comes first ("ab" before "abc"). A character with a code above 127 comes after all the plain keyboard ones. There is no order for yes-or-no values, structs or references that GBML knows, so those are refused when the project is checked.

See also: array_sortgrid_sortgrid_sort_rowslist_shufflestring_code_atlist_lengthlist_copylist_foreachListsData containers